<?php

namespace app\common\model\goods;

use app\common\model\goods\category\Region;
use app\common\model\user\Info;
use think\Model;
use traits\model\SoftDelete;

class Goods extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'goods';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text',
        'image_str',
        'images_array',
        'min_price',
        'min_original_price',
        // 'is_pay',
    ];
    protected static function init()
    {
        self::afterInsert(function ($row) {
            $row->save(['weigh' => $row['id']]);
        });
    }
    public function getMinPriceAttr($v, $d)
    {
        return Spec::where('goods_id', $d['id'])->min('price');
    }
    public function getMinOriginalPriceAttr($v, $d)
    {
        return Spec::where('goods_id', $d['id'])->min('original_price');
    }
    // public function getIsPayAttr($v, $d)
    // {
    //     if ($d['type'] == '2') {
    //         $info = Info::get([
    //             'province' => $d['province'],
    //             'city' => $d['city'],
    //             'area' => $d['area'],
    //             'address' => $d['address'],
    //         ]);
    //         if ($info) {
    //             return false;
    //         }
    //     }
    //     return true;
    // }
    public function getImageStrAttr($v, $d)
    {
        return $d['image'] ? cdnurl($d['image'], true) : '';
    }
    public function getImagesArrayAttr($v, $d)
    {
        if ($d['images']) {
            $array = explode(',', $d['images']);
            foreach ($array as &$value) {
                $value = $value ? cdnurl($value, true) : '';
            }
            return $array;
        }
        return [];
    }
    public function getTypeList()
    {
        return ['1' => __('普通商品'), '2' => __('区域商品')];
    }
    public function getHotList()
    {
        return ['1' => __('是'), '0' => __('否')];
    }
    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
    }
    public function spec()
    {
        return $this->hasMany(Spec::class, 'goods_id', 'id');
    }

    public function region()
    {
        return $this->belongsTo(Region::class, 'region_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function getHotTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getHotList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }
}
